﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IRIS.Models;

namespace IRIS.Controllers
{
    public class FAQController : Controller
    {
        //
        // GET: /FAQ/
        DataRepo m_Db = new DataRepo();

        [HttpGet]
        //Returns all FAQ to view, if there is no questions it will be empty
        public ActionResult Index()
        {
            var fData = m_Db.getFAQs();
            if (fData != null)
            {
                return View(fData);
            }
            return View();
        }

        [Authorize(Roles = "Stjórnendur")]
        [HttpGet]
        public ActionResult List()
        {
            var fData = m_Db.getFAQs();
            return View(fData);
        }

        [Authorize(Roles ="Stjórnendur")]
        [HttpGet]
        public ActionResult Create()
        {
            FAQ fData = new FAQ();

            return View(fData);
        }

        [Authorize(Roles = "Stjórnendur")]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(FAQ fData)
        {
            if (string.IsNullOrEmpty(fData.Title))
                ModelState.AddModelError("Title", "Titill verður að vera á Spurningu.");
            if (string.IsNullOrEmpty(fData.Question))
                ModelState.AddModelError("Question", "Spurning verður að vera útfyllt");
            if (fData.Answer == "<br>")
                ModelState.AddModelError("Answer", "Svar við spurningu verður að vera til staðar.");

            try
            {
                if (ModelState.IsValid)
                {
                    //everything is alright and we commit to database
                    fData.Alive = true;
                    m_Db.addFAQ(fData);
                    return RedirectToAction("Index");
                }
                else
                {
                    //something is wrong and we return the model back with error state
                    return View(fData);
                }
            }
            catch
            {
                //Something is not o.k so we return error state
                return View(fData);
            }
        }
        [Authorize(Roles = "Stjórnendur")]
        [HttpGet]
        public ActionResult Edit(int? id, Boolean delete)
        {
            if (id.HasValue)
            {
                int realId = id.Value;
                var fData = m_Db.getFAQById(realId);
             
                if (fData != null && delete == true)
                {
                    fData.Alive = false;
                    m_Db.editFAQ(realId, fData);
                    return RedirectToAction("List");
                }
                else if (fData != null && delete == false)
                {
                    return View(fData);
                }
             }
            return View("NotFound");

        }
        [Authorize(Roles = "Stjórnendur")]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int id, FAQ theData)
        {
            if (string.IsNullOrEmpty(theData.Title))
                ModelState.AddModelError("Title", "Titill verður að vera á Spurningu.");
            if (string.IsNullOrEmpty(theData.Question))
                ModelState.AddModelError("Question", "Spurning verður að vera útfyllt");
            if (theData.Answer == "<br>")
                ModelState.AddModelError("Answer", "Svar við spurningu verður að vera til staðar.");

            try
            {
                if (ModelState.IsValid)
                {
                    //everything is alright and we commit to database
                    m_Db.editFAQ(id, theData);
                    return RedirectToAction("Index");
                }
                else
                {
                    //something is wrong and we return the model back with error state
                    return View(theData);
                }
            }
            catch
            {
                //Something is not o.k so we return error state
                return View(theData);
            }  
        }

    }
}
